package com.jxwifi.cloud.quickcleanserver.mycenter;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jxwifi.cloud.quickcleanserver.R;
import com.jxwifi.cloud.quickcleanserver.app.AccountMgr;
import com.jxwifi.cloud.quickcleanserver.app.CleanBasicActivity;
import com.jxwifi.cloud.quickcleanserver.bean.WithdraApplyBean;
import com.jxwifi.cloud.quickcleanserver.bean.WithdraApplyEventBean;
import com.jxwifi.cloud.quickcleanserver.utils.BankCardUtils;
import com.jxwifi.cloud.quickcleanserver.utils.BankInfo;
import com.jxwifi.cloud.quickcleanserver.utils.ToastUtils;

import org.simple.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.trinea.android.common.util.REPattern;

/**
 * Created by Administrator on 2020/6/24.
 */

public class AddCardActivity extends CleanBasicActivity {

    @Bind(R.id.et_my_authentication_name)
    EditText et_my_authentication_name;
    @Bind(R.id.et_cash_pank_number)
    EditText et_cash_pank_number;
    @Bind(R.id.tv_my_authentication_city)
    EditText tv_my_authentication_city;
    @Bind(R.id.add_card)
    TextView add_card;
    private int flag;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bank_card);
        ButterKnife.bind(this);
        openEventBus();
        flag = getIntent().getIntExtra("flag",0);
        Log.i(TAG,"flag = "+flag);
        //initView();
    }

    private void initView() {

        et_cash_pank_number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //无法识别出银行信息，有空再看
                int huoqu = et_cash_pank_number.getText().toString().length();
                if (huoqu >= 6) {
                    String huoqucc = et_cash_pank_number.getText().toString();
                    char[] cardNumber = huoqucc.toCharArray();
                    String name = BankInfo.getNameOfBank(cardNumber, 0);// 获取银行卡的信息
                    tv_my_authentication_city.setText(null);
                } else {
                    tv_my_authentication_city.setText(null);
                }

                /*String cardNum = et_cash_pank_number.getText().toString().trim();
                Log.i(TAG,"initView() ==> cardNum = "+cardNum);
                if (s.length()<6){
                    tv_my_authentication_city.setText("");
                } else if (cardNum.length() == 6) {
                    String nameOfBank = BankCardUtils.getNameOfBank(cardNum);
                    tv_my_authentication_city.setText(nameOfBank);
                }else if (cardNum.length() == 8) {
                    String nameOfBank = BankCardUtils.getNameOfBank(cardNum);
                    if (!TextUtils.isEmpty(nameOfBank)) {
                        tv_my_authentication_city.setText(nameOfBank);
                    }
                } else if (cardNum.length() > 15) {
                    String nameOfBank = BankCardUtils.getDetailNameOfBank(cardNum);
                    if (!TextUtils.isEmpty(nameOfBank)) {
                        tv_my_authentication_city.setText(nameOfBank);
                    }
                }*/
            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        });
    }

    @OnClick({R.id.add_card,R.id.rel_img_back_pressed})

    void clickCK(View v) {
        switch (v.getId()) {
            //返回
            case R.id.rel_img_back_pressed:

                onBackPressed();

                break;
           case R.id.add_card:
               if (TextUtils.isEmpty(et_my_authentication_name.getText().toString())){
                   Toast.makeText(mBaseContext,"收款人不能为空",Toast.LENGTH_SHORT).show();
                   Toast toast =Toast.makeText(mBaseContext, "收款人不能为空", Toast.LENGTH_SHORT);
                   ToastUtils.showMyToast(toast,3000);
                   return;
               }
               if (TextUtils.isEmpty(et_cash_pank_number.getText().toString())){
                   Toast.makeText(mBaseContext,"银行卡号不能为空",Toast.LENGTH_SHORT).show();
                   Toast toast =Toast.makeText(mBaseContext, "银行卡号不能为空", Toast.LENGTH_SHORT);
                   ToastUtils.showMyToast(toast,3000);
                   return;
               }
               if (TextUtils.isEmpty(tv_my_authentication_city.getText().toString())){
                   Toast toast =Toast.makeText(mBaseContext, "开户行号不能为空", Toast.LENGTH_SHORT);
                   ToastUtils.showMyToast(toast,3000);
                   return;
               }

               if(flag == 1){//从我的余额界面过来
                   Intent intent = new Intent(mBaseContext, WithdrawalActivity.class);
                   intent.putExtra("flag",2);
                   intent.putExtra("realName",et_my_authentication_name.getText().toString());
                   intent.putExtra("bankName",tv_my_authentication_city.getText().toString());
                   intent.putExtra("bankNum",et_cash_pank_number.getText().toString());
                   startActivity(intent);
               }else{
                   EventBus.getDefault().post(new WithdraApplyEventBean(et_my_authentication_name.getText().toString(),tv_my_authentication_city.getText().toString(),et_cash_pank_number.getText().toString()));
               }
               finish();
            break;

        }
    }
}
